﻿using Microsoft.Extensions.Caching.Memory;

namespace SSO.Web1.Utils;

/// <summary>
/// 缓存帮助类-真正项目这里用redis
/// </summary>
public class CacheHelper (IMemoryCache memoryCache) {
    //内存缓存

    /// <summary>
    /// 设置缓存
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <param name="expiry"></param>
    /// <returns></returns>
    public void StringSet<T> (string key, T value, TimeSpan? expiry = null) {
        if (expiry == null) {
            memoryCache.Set(key, value);
        } else {
            memoryCache.Set(key, value, (TimeSpan)expiry);
        }
    }

    /// <summary>
    /// 获取缓存
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <returns></returns>
    public T StringGet<T> (string key) {
        T result = memoryCache.Get<T>(key);
        return result;
    }

    public void DeleteKey (string key) {
        memoryCache.Remove(key);
    }
}